package com.example.exercise.demo.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;

import com.example.exercise.demo.vo.AppPapersVO;
import com.example.exercise.demo.dao.entity.AppPapersEntity;
import com.example.exercise.demo.dao.AppPapersDAO;

/**
* 
*/
@Service
public class AppPapersService {

    @Autowired
    private AppPapersDAO appPapersDAO;

    public List<AppPapersVO> findByWhere(Page<?> page, AppPapersVO appPapersVO){
        List<AppPapersVO> list = new ArrayList<>();
        List<AppPapersEntity> list0 = appPapersDAO.findByWhere(page, appPapersVO);
        for (AppPapersEntity appPapersEntity : list0) {
            list.add(toAppPapersVO(appPapersEntity));
        }
        return list;
    }

    public AppPapersVO findByKey(AppPapersVO appPapersVO){
        AppPapersEntity appPapersEntity = appPapersDAO.findByKey(appPapersVO);
        return toAppPapersVO(appPapersEntity);
    }

    public int insert(AppPapersVO appPapersVO){
        return appPapersDAO.insert(toAppPapersEntity(appPapersVO));
    }

    public int updateByKey(AppPapersVO appPapersVO){
        return appPapersDAO.updateByKey(toAppPapersEntity(appPapersVO));
    }

    public int deleteByKey(AppPapersVO appPapersVO){
        return appPapersDAO.deleteByKey(appPapersVO);
    }

    private AppPapersVO toAppPapersVO(AppPapersEntity appPapersEntity) {
        AppPapersVO appPapersVO = new AppPapersVO();
                appPapersVO.setId(String.valueOf(appPapersEntity.getId()));
        appPapersVO.setScanCount(String.valueOf(appPapersEntity.getScanCount()));
        appPapersVO.setUserId(String.valueOf(appPapersEntity.getUserId()));
        appPapersVO.setTitle(appPapersEntity.getTitle());
        appPapersVO.setContext(appPapersEntity.getContext());
        appPapersVO.setStatus(String.valueOf(appPapersEntity.getStatus()));
        appPapersVO.setIfHomepage(String.valueOf(appPapersEntity.getIfHomepage()));
        appPapersVO.setKindType(appPapersEntity.getKindType());

        return appPapersVO;
    }

    private AppPapersEntity toAppPapersEntity(AppPapersVO appPapersVO) {
        AppPapersEntity appPapersEntity = new AppPapersEntity();
                String id = appPapersVO.getId();
        if(id != null) {
            appPapersEntity.setId(Integer.parseInt(appPapersVO.getId()));
        }
        String scanCount = appPapersVO.getScanCount();
        if(scanCount != null) {
            appPapersEntity.setScanCount(Integer.parseInt(appPapersVO.getScanCount()));
        }
        String userId = appPapersVO.getUserId();
        if(userId != null) {
            appPapersEntity.setUserId(Integer.parseInt(appPapersVO.getUserId()));
        }
        appPapersEntity.setTitle(appPapersVO.getTitle());
        appPapersEntity.setContext(appPapersVO.getContext());
        String status = appPapersVO.getStatus();
        if(status != null) {
            appPapersEntity.setStatus(Integer.parseInt(appPapersVO.getStatus()));
        }
        String ifHomepage = appPapersVO.getIfHomepage();
        if(ifHomepage != null) {
            appPapersEntity.setIfHomepage(Integer.parseInt(appPapersVO.getIfHomepage()));
        }
        appPapersEntity.setKindType(appPapersVO.getKindType());

        return appPapersEntity;
    }

}